package com.cms.user;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import com.cms.bean.UserBean;
import com.cms.database.SQLString;

public class UserHelper {

	@SuppressWarnings("finally")
	public static int checkUserLogin(Connection conn,String user,String pwd){
		CallableStatement stmt = null;
		int return_code = 0;
		try {
			stmt = conn.prepareCall(SQLString.CHECK_USER_LOGIN);
			stmt.registerOutParameter(1, Types.INTEGER);
			stmt.setString(2, user);
			stmt.setString(3, pwd);
			stmt.execute();
			return_code = stmt.getInt(1);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
            if (stmt != null) {
                try {
                    stmt.close();
                    stmt = null;
                } catch (final SQLException e) {}
            }
            return return_code;
		}

	}
	
	@SuppressWarnings("finally")
	public static UserBean getUserBeanByUser(Connection conn,String user){
		PreparedStatement stmt = null;
		ResultSet rs = null;
		UserBean ub = new UserBean();
		try {
			stmt = conn.prepareStatement(SQLString.GET_USER_INFO_BY_USER);
			stmt.setString(1, user);
			rs = stmt.executeQuery();
			while(rs.next()){
				ub.setId(rs.getInt("id"));
				ub.setUser(user);
				ub.setName(rs.getString("user_name"));
				ub.setWorkPhone(rs.getString("work_phone"));
				ub.setMobilePhone(rs.getString("mobile_phone"));
				ub.setEmail(rs.getString("email"));
				ub.setCreateDate(rs.getTimestamp("create_date"));
				ub.setCreateUser(rs.getString("create_user"));
				ub.setAdminType(rs.getString("admin_type"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
            if (rs != null) {
                try {
                	rs.close();
                	rs = null;
                } catch (final SQLException e) {}
            }
            if (stmt != null) {
                try {
                    stmt.close();
                    stmt = null;
                } catch (final SQLException e) {}
            }
            return ub;
		}
		
	}
}
